package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.voicechat.firstmet.MainActivity;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.MyTaskAdapter;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.CharmBean;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.PersonDataBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.net.service.PersonDataService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: MyAssignmentsActivity
 * @Description: 我的任务页面（新手任务）
 * @Author: ljp
 * @CreateDate: 2018/10/11 20:26
 * @Version: 1.0
 */
public class MyAssignmentsActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    /* @BindView(R.id.tv_data_state)
     TextView tvDataState;
     @BindView(R.id.tv_setvoice_state)
     TextView tvSetvoiceState;
     @BindView(R.id.tv_voice_chat_state)
     TextView tvVoiceChatState;
     @BindView(R.id.tv_like_state)
     TextView tvLikeState;
     @BindView(R.id.tv_sendgif_state)
     TextView tvSendgifState;
     @BindView(R.id.tv_realname_state)
     TextView tvRealnameState;*/
    @BindView(R.id.recy_task)
    RecyclerView recyTask;

    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String id;
    private String deviceInfo;
    private String TAG = "MyAssignmentsActivity";
    private List<CharmBean.ObjectBean> objectBeans;
    private int[] taskIcon;
    private MyTaskAdapter myTaskAdapter;

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_my_assignments;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        id = mySeleInfo.getId();
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();
    }

    /**
     * @return
     * @method
     * @description 数据请求
     * @date: 2018/11/18 13:32
     * @author: ljp
     */
    private void initHttpTask() {
        MeInfoService.selectNewTask(id, token, deviceInfo).subscribe(charmBean -> {
            if (charmBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                objectBeans.addAll(charmBean.getList());
                myTaskAdapter.notifyDataSetChanged();
            }else if(charmBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(charmBean.getMsg());
            }
            Log.i(TAG, "initHttpTask: ------" + charmBean.getList().size());
        }, throwable -> {
            Log.i(TAG, "initHttpTask: ------" + throwable.toString());
        });
    }
    /**
     * @return
     * @method
     * @description 数据控件实例化
     * @date: 2018/11/18 13:31
     * @author: ljp
     */
    private void initView() {
        taskIcon = new int[]{R.mipmap.task_person, R.mipmap.task_set, R.mipmap.task_set_voice, R.mipmap.task_like, R.mipmap.task_send_gif, R.mipmap.task_real_name};
        objectBeans = new ArrayList<>();
        myTaskAdapter = new MyTaskAdapter(activity, objectBeans, R.layout.item_task);
        myTaskAdapter.setTaskIcon(taskIcon);
        recyTask.setLayoutManager(new LinearLayoutManager(activity));
        recyTask.setAdapter(myTaskAdapter);
        myTaskAdapter.setItemClick(new MyTaskAdapter.ItemClick() {
            @Override
            public void setItemState(CharmBean.ObjectBean objectBean) {
                // 状态 (我的任务界面)   0 未领取  1 已经取 2未完成
                if(objectBean.getState() == 0){//领取奖励
                    setHttpGet(objectBean);
                }else if(objectBean.getState() == 2){
                    skipJude(objectBean.getName());
                }
            }
        });
        initHttpTask();
    }
    /**
    * @method
    * @description 完成任务跳转页面判断
    * @date: 2018/11/18 14:32
    * @author: ljp
    * @return
    */
    private void skipJude(String name){
        Intent intent = null;
        switch (name){
            case "语音介绍":
                intent = new Intent(activity,PersonDataSetActivity.class);
                startActivity(intent);
                finish();
                break;
            case "语音通话(60s以上)":
                intent = new Intent(activity,MainActivity.class);
                intent.putExtra("flag",1);
                startActivity(intent);
                finish();
                break;
            case "社区点赞":
                intent = new Intent(activity,MainActivity.class);
                intent.putExtra("flag",0);
                startActivity(intent);
                finish();
                break;
            case "赠送礼物":
                intent = new Intent(activity,MainActivity.class);
                intent.putExtra("flag",1);
                startActivity(intent);
                finish();
                break;
            case "实名认证":
                judeRealName();
                //intent = new Intent(activity,RealNameActivity.class);
                break;
        }

    }
    /**
     * @return
     * @method
     * @description 实名认证状态判断
     * @date: 2018/11/6 15:07
     * @author: ljp
     */
    private void judeRealName() {
        Log.i(TAG, "judeRealName: --getIsAttestation---" + mySeleInfo.getIsAttestation());
        Intent intent = null;
        //判断是否提交过实名认证信息
        if (mySeleInfo.getIsAttestation() == 0) {// 实名认证任务 0未认证 1 已审核 2 审核中 3驳回
            intent = new Intent(activity, RealNameActivity.class);
            startActivity(intent);
        } else {//上传过就去判断，实名认证的当前的状态
            MeInfoService.certificationSelect(id, token, PhoneUtils.getDeviceInfo(context)).subscribe(personDataBean -> {
                Log.i(TAG, "judeRealName: -----personDataBean----" + personDataBean.getObject().toString());
                if (personDataBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                    //判断state状态  认证状态 0审核中 1审核通过 2驳回
                    PersonDataBean.ObjectBean object = personDataBean.getObject();
                    if (object.getStatus() == 0) {
                        toast_MakeText(getResources().getString(R.string.real_name));
                    } else if (object.getStatus() == 1) {
                        Intent intent2 = new Intent(activity, RealNameActivity.class);
                        intent2.putExtra("object", object);
                        startActivity(intent2);
                    } else if (object.getStatus() == 2) {
                        toast_MakeText(getResources().getString(R.string.real_name_no));
                        Intent intent3 = new Intent(activity, RealNameActivity.class);
                        startActivity(intent3);
                    }
                } else if (personDataBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                    Utils.setToken(context);
                } else {
                    toast_MakeText(personDataBean.getMsg());
                }
            }, throwable -> {
                Log.i(TAG, "judeRealName: ---------" + throwable.toString());

            });

        }
    }
    /**
    * @method
    * @description 领取奖励
    * @date: 2018/11/18 14:30
    * @author: ljp
    * @return
    */
    private void setHttpGet(CharmBean.ObjectBean objectBean) {
        MeInfoService.receiveAward(id,token,deviceInfo,objectBean.getActivityId(),objectBean.getGold()+"", PersonDataService.GIF_TYPE.GIF_TASK).subscribe(result -> {
            Log.i(TAG, "setHttpGet: ----"+result.toString());
            if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                objectBean.setState(1);
                myTaskAdapter.notifyDataSetChanged();
            } else if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(result.getMsg());
            }
        },throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
            Log.i(TAG, "setHttpGet: -----"+throwable.toString());
        });


    }
    @OnClick(R.id.linear_back)
    public void onClick() {
        finish();
    }
}
